package br.com.commons.utility.datatable.builder;

import java.util.Map;

import br.com.commons.utility.datatable.DataTable;

public class DataTableBuilder {

	private DataTable dataTable;
	private Map<String, Object> dados;
	
	public DataTableBuilder(Map<String, Object> dados){
		this.dados = dados;
	}
	
	public DataTable getDataTable(){		
		dataTable = new DataTable();
		dataTable.setsEcho(getSEcho());
		dataTable.setSearchValue(getSearchValue());
		dataTable.setOrderColumn(getOrderColumn());
		dataTable.setOrderDirection(getOrderDirection());
		dataTable.setFirstPosition(getFirstPosition());
		dataTable.setNumberOfRecords(getNumberOfRecords());
		dataTable.setNumberColumns(getNumberColumns());
		dataTable.setSearchColumns(getSearchColumns());
		return dataTable;
	}
	
	private String getSEcho(){
		return  dados.get("sEcho").toString();
	}
	
	private String getSearchValue(){
		return  dados.get("sSearch").toString();
	}
	
	private Integer getFirstPosition(){
		return Integer.parseInt(dados.get("iDisplayStart").toString());
	}
	
	private Integer getNumberOfRecords(){
		return Integer.parseInt(dados.get("iDisplayLength").toString());
	}
	
	private Boolean[] getSearchColumns(){			
		return getObjectBooleanFromMap("bSearchable_");
	}
	
	private Integer getOrderColumn(){		
		return Integer.parseInt(dados.get("iSortCol_0").toString());
	}
	
	private String getOrderDirection(){
		return dados.get("sSortDir_0").toString();
	}
	
	private Integer getNumberColumns(){
		return Integer.parseInt(dados.get("iColumns").toString());
	}
	
	private Boolean[] getObjectBooleanFromMap(String key){
		Boolean[] objects = new Boolean[ getNumberColumns()];	    
		for (int i = 0; i <  getNumberColumns(); i++) {
			objects[i] = Boolean.parseBoolean(dados.get(key + i).toString());
		}
		return objects;
	}
}
